package xyz.xpman.prisonerfitness.sqlite;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.util.Consumer;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

@SuppressWarnings("unused")
public class DbHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "jxust_timor_demo.db";
    private static final int DB_VERSION = 1;

    private final Map<Integer, @NotNull Consumer<SQLiteDatabase>> upgradeMapping = Map.ofEntries(
            Map.entry(1, this::upgradeTo1)
    );



    public DbHelper(@Nullable Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public DbHelper(@Nullable Context context, @Nullable SQLiteDatabase.CursorFactory factory) {
        super(context, DB_NAME, factory, DB_VERSION);
    }

    public DbHelper(@Nullable Context context, @Nullable SQLiteDatabase.CursorFactory factory, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, DB_NAME, factory, DB_VERSION, errorHandler);
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public DbHelper(@Nullable Context context, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, DB_NAME, DB_VERSION, openParams);
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE user(\n"
                + "id integer PRIMARY KEY AUTOINCREMENT NOT NULL, \n"
                + "username varchar(16) NOT NULL, \n"
                + "password varchar(40) NOT NULL, \n"
                + "role varchar(32), \n"
                + "sex varchar(2) \n"
                + ")");

        // default user for test
        // String password = EncryptUtil.encryptSha("password");
        db.execSQL("INSERT into user(id, username, password, sex, role) "
                +  "VALUES (666666, 'root', 'e3d7032d35f5de0b2e2a04505dcc06e23be645c0', '男', 'root')" );




        // add table: training_records
        db.execSQL("CREATE TABLE training_records( \n"
                + "id integer PRIMARY KEY AUTOINCREMENT NOT NULL, \n"
                + "groups integer NOT NULL, \n"
                + "usedTime integer NOT NULL, \n"
                + "isWarmUp smallint NOT NULL, \n"
                + "date varchar(24), \n"
                + "difficulty integer NOT NULL, \n"
                + "times integer NOT NULL, \n"
                + "movesId varchar(32) NOT NULL, \n"
                + "chapterId varchar(32) NOT NULL \n"
                + ")");



        // add table: training_thoughts
        db.execSQL("CREATE TABLE training_thoughts( \n"
                + "id integer PRIMARY KEY AUTOINCREMENT NOT NULL, \n"
                + "date varchar(32), \n"
                + "thoughts varchar(1024) NOT NULL \n"
                + ")");
    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("DbHelper", "oldVersion: " + oldVersion + ", newVersion: " + newVersion);

        for (int i = oldVersion; i <= newVersion; i++) {
            Consumer<SQLiteDatabase> func = upgradeMapping.get(i);

            if (null != func) {
                Log.i("DbHelper", "onUpgrade: " + i);
                func.accept(db);
            }
        }
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    private void upgradeTo1(SQLiteDatabase db) {

    }

}
